/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.google.code.jmarias.client.phases;

import com.google.code.jmarias.client.Entity;
import com.google.code.jmarias.client.Globals;
import com.google.code.jmarias.client.GameLocals;
import com.google.code.jmarias.client.components.AbstractComponent;
import com.google.code.jmarias.client.components.ButtonRenderComponent;
import com.google.code.jmarias.client.tcp.RemoteServer;
import com.google.code.jmarias.common.action.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.state.StateBasedGame;

/**
 *
 * @author bart
 */
public abstract class AbstractPhase {
    protected Globals globals;
    protected GameLocals locals;
    // Instance pripojeni k serveru
    protected RemoteServer server;

   // Fronta komponent ktere jsou provadeny po sobe
    Queue<AbstractComponent> serialComponents;

    protected Image actionButtonIMG;

    Map<Class, String> actionButtonInfo = new HashMap<Class, String>() {
      {
        put(ChooseFromPeopleAction.class, "Zvolit trumf z lidu.");
        put(ChooseDurchAction.class, "Durch!");
        put(ChooseBetlAction.class, "Betl!");
        put(ChooseSuitAction.class, "Hra!");
      }
    };

    public abstract void update(GameContainer gc, StateBasedGame sb, int delta) throws SlickException;

    public abstract void render(GameContainer gc, StateBasedGame sb, Graphics gr) throws SlickException;

    public void refusePickedCards() {
        return;
    }

    /** Prida komponentu do fronty seriovych komponent */
    public void addSerialComponent(AbstractComponent component) {
        serialComponents.offer(component);
    }

    public ArrayList<Entity> createActionButtons(List<Class> actions) throws SlickException {
        ArrayList<Entity> actionButtons = new ArrayList<Entity>();

        actionButtonIMG = new Image(globals.getIMGPATH() + "cudlikAkce.png");
        Image image = actionButtonIMG.getSubImage(0, 0, 150, 50);
        Image imageHover = actionButtonIMG.getSubImage(150, 0, 150, 50);


        for(int i=0; i < actions.size(); i++) {
                System.out.println(i + " Vytvarim tlacitko: " + actions.get(i) + ":" + actionButtonInfo.get(actions.get(i)));
                actionButtons.add(new Entity(actionButtonInfo.get(actions.get(i))));
        }

        for(int i=0; i < actionButtons.size(); i++ ) {
             System.out.println("Nastavuji tlacitko: " + i);
             actionButtons.get(i).setPosition(new Vector2f(100f, 200f + i * 55f));
             actionButtons.get(i).setScale(1.0f);
             actionButtons.get(i).AddComponent(new ButtonRenderComponent(i, globals, image, imageHover));

        }

        return actionButtons;         
    }

    public void controlActionButtons(Input input, ArrayList<Entity> actionButtons, List<Class> actions) {
        if( actionButtons.size() != 0) {


          int mouseX = input.getMouseX();
          int mouseY = input.getMouseY();

          for(int i = 0; i < actionButtons.size(); i++) {
            if (( mouseX >= actionButtons.get(i).getPosition().x && mouseX <= actionButtons.get(i).getPosition().x + actionButtonIMG.getWidth()/2f) &&
                ( mouseY >= actionButtons.get(i).getPosition().y && mouseY <= actionButtons.get(i).getPosition().y + actionButtonIMG.getHeight() )) {
                  globals.setHoverID(i+1);
                  if(input.isMousePressed(Input.MOUSE_LEFT_BUTTON)) {
                        try {
                            server.sendAction((AbstractAction) actions.get(globals.getHoverID()-1).newInstance());
                        } catch (InstantiationException ex) {
                            Logger.getLogger(AbstractPhase.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (IllegalAccessException ex) {
                            Logger.getLogger(AbstractPhase.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        System.out.println(actions.get(globals.getHoverID()-1));
                  }
            }
          }


        /* Obsluha klavesnice a kliknuti mysi. */
        if(input.isKeyPressed(Input.KEY_DOWN)) {
            globals.setHoverID((globals.getHoverID() + actionButtons.size() + 2) % (actionButtons.size() + 1));
        } else if(input.isKeyPressed(Input.KEY_UP)) {
            globals.setHoverID((globals.getHoverID() + actionButtons.size()) % (actionButtons.size() + 1));
        } else if(globals.getHoverID() > 0 && input.isKeyPressed(Input.KEY_ENTER) ) {
            System.out.println(actions.get(globals.getHoverID() - 1));
            try {
                server.sendAction((AbstractAction) actions.get(globals.getHoverID()-1).newInstance());
            } catch (InstantiationException ex) {
                Logger.getLogger(AbstractPhase.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(AbstractPhase.class.getName()).log(Level.SEVERE, null, ex);
            }
        }


       }


       if(input.isMousePressed(Input.MOUSE_LEFT_BUTTON)) {
                                globals.setHoverID(0);
       }
    }

    public void updateActionButtons(ArrayList<Entity> actionButtons, GameContainer gc, StateBasedGame sb, int delta) {
        for(int i = 0; i < actionButtons.size(); i++) {
            actionButtons.get(i).update(gc, sb, delta);
        }        
    }

    public void renderActionButtons(ArrayList<Entity> actionButtons, GameContainer gc, StateBasedGame sb, Graphics gr) {
        for(int i = 0; i < actionButtons.size(); i++) {
             actionButtons.get(i).render(gc, sb, gr);
             Vector2f positionTMP = new Vector2f(actionButtons.get(i).getPosition().x + 5f,
                                                 actionButtons.get(i).getPosition().y + actionButtonIMG.getHeight() / 2f - 10f);
             globals.printText("small", positionTMP, Color.white, actionButtons.get(i).getId());
        }

    }

}
